package com.foreveross.fetl.web.action.preprocess;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.IHbasePageApplication;
import com.foreveross.fetl.application.ISourceApplication;
import com.foreveross.fetl.common.vo.model.doub.DoublePlaneInfo;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.vo.SourceVO;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("planeInfoAction")
@Scope("prototype")
public class PlaneInfoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5951909785585086241L;
	
	
	@Inject
	private IHbasePageApplication hbasePageApplication;
	
	@Inject ISourceApplication sourceApplication;
	
	@SuppressWarnings("unchecked")
	public String pageJson() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String type=req.getParameter("type");
		String channel = req.getParameter("channel");
		String fromCity=req.getParameter("fromCity");
		String toCity=req.getParameter("toCity");
		String flightDate=req.getParameter("flightDate");
		String returnDate=req.getParameter("returnDate");
		String start = req.getParameter("start");
		String end = req.getParameter("end");
		Date startTime =null;
		Date endTime=null;
		if(null!=start && !"".equals(start)){
			start=start.replace("-", "").replace(" ", "");
			startTime=DateUtils.parse(start, "yyyyMMddHH");
		}
		if(null!=end && !"".endsWith(end)){
			end=end.replace("-", "").replace(" ", "");
			endTime=DateUtils.parse(end, "yyyyMMddHH");
		}
		if(null!=flightDate){
			flightDate=flightDate.replace("-", "").replace(" ", "");
		}
		if(null!=returnDate){
			returnDate=returnDate.replace("-", "").replace(" ", "");
		}
		
		Page<DoublePlaneInfo> all=hbasePageApplication.pageSingleQuery(type, channel, startTime, endTime, fromCity,
				toCity, flightDate, returnDate, page, pagesize);
		List<DoublePlaneInfo> list=all.getResult();
		if(null!=list && list.size()>0){
			Collections.sort(list, new Comparator(){
				@Override
				public int compare(Object o1, Object o2) {
					try {
						DoublePlaneInfo vo1=(DoublePlaneInfo) o1;
						DoublePlaneInfo vo2=(DoublePlaneInfo) o2;
						if(null!=vo1.getTotalLowestPrice() && vo1.getTotalLowestPrice()>0)
							return vo1.getTotalLowestPrice().compareTo(vo2.getTotalLowestPrice());
						else
							return vo1.getSumLowestPrice().compareTo(vo2.getSumLowestPrice());
					} catch (Exception e) {
						e.printStackTrace();
					}
					return 0;
				}
				
			});
		}
		dataMap.put("Rows", all.getResult());
		dataMap.put("start", page * pagesize - pagesize);
		dataMap.put("limit", pagesize);
		dataMap.put("Total", all.getTotalCount());
		return "JSON";
	}
	
	public String list(){
		
		return "METHOD";
	}
	
	public String listSourceSel(){
		List<SourceVO> listSource=sourceApplication.listAll();
		dataMap.put("listSource", listSource);
		return "JSON";
	}
	
	public String view(){
		return "METHOD";
	}
	
	public String get() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String id=req.getParameter("id");
		dataMap.put("data", hbasePageApplication.getByRowkey(id));
		return "JSON";
	}
	
	
}
